var Dashboard = {};
Dashboard.create = {};
Dashboard.create.txt_name = $$id("dashboard.create.txt_name");
Dashboard.create.submit = $$id("dashboard.create.submit");

Dashboard.slt_list = $$id("dashboard.list")

Dashboard.Workspace = {};
Dashboard.Workspace.list_containers = $$id("dashboard.workspace.list_containers");
Dashboard.Workspace.name = $$id("dashboard.workspace.name");
Dashboard.Workspace.delete_btn = $$id("dashboard.workspace.delete")
Dashboard.Workspace.add_container_btn = $$id("dashboard.workspace.add_container")
Dashboard.Workspace.add_container_txt = $$id("dashboard.workspace.add_container_text")
Dashboard.currentWorkspace = null;

var Template = {};
Template.container = $$id("template.container").innerHTML.toString();

var Workspace = chrome.extension.getBackgroundPage().Workspace;
var SearchContainer = chrome.extension.getBackgroundPage().SearchContainer;

Dashboard.createWorkspace = function()
{
    var txtin = Dashboard.create.txt_name;
    var name = txtin.value;
    
    var work = Workspace.createWorkspace(name);
    console.log("[Log] Adding workspace '"+name+"'")
    Dashboard.refresh();
};


Dashboard.refreshListWorkspace = function()
{
    var select = Dashboard.slt_list;
    while (select.options.length)
        select.remove(0);

    var workspaces = Workspace.instances.clone().sort(Workspace.sortByName);
    for (var i = 0; i < workspaces.length; i++)
    {
        var opt = new Option(workspaces[i].getName(),workspaces[i].getId());
        select.add(opt);
    }

};

Dashboard.selectWorkspace = function(e)
{
    var slt = e.srcElement;
    var opt = slt.options[slt.selectedIndex];
    var name = opt.text;
    Dashboard.Workspace.name.innerHTML = name;

    var id = opt.value;

    var w = Workspace.getById(id);

    Dashboard.currentWorkspace = w;

    Dashboard.refreshWorkspace();
}

Dashboard.refreshWorkspace = function()
{
    var w = Dashboard.currentWorkspace;
    Dashboard.Workspace.list_containers.innerHTML = "";
    if (w)
    {
        var containers = w.getAllContainers();
        Dashboard.displayContainers(containers,0)
    }
};
Dashboard.addContainerToContainer = function(e)
{
  var o = e.srcElement;
  var container = o.container;
  var name = window.prompt("Entrez le nouveau nom de conteneur");
  var sc = new SearchContainer(name);
  container.addContainer(sc);
  Dashboard.refreshWorkspace();
  return false;
}
Dashboard.deleteContainerOfContainer = function(e)
{
  var o = e.srcElement;
  var container = o.container;
  Dashboard.currentWorkspace.removeContainer(container);
	console.log("Delete? Oo")
  Dashboard.refreshWorkspace();
};
Dashboard.changeNameContainer = function(e)
{
  var o = e.srcElement;
  var container = o.container;
  var name = window.prompt("Entrez le nouveau nom de conteneur '"+container.getName()+"'");
  container.setName(name);
  Dashboard.refreshWorkspace();
  return false;
};
Dashboard.displayContainers = function(containers, indent)
{
    for (var i = 0; i < containers.length; i++)
    {
        var c = containers[i];
        var div = document.createElement("div");
        var o = {
            name: c.getName(),
            id: 0
        };
        div.innerHTML = Template.container.replaceVars(o);
        div.container = c;
        div.currentIndent = indent;
        var links = div.getElementsByTagName("a");
        for (var j = 0; j < links.length; j++)
        {
          var action = links[j].getAttribute("action");
          switch(action)
          {
            case "add":
              links[j].onclick = Dashboard.addContainerToContainer
              links[j].container = c;
              break;
            case "delete":
              links[j].onclick = Dashboard.deleteContainerOfContainer;
              links[j].container = c;
              break;
            case "change":
              links[j].onclick = Dashboard.changeNameContainer;
              links[j].container = c;
              break;
            default:
              continue;
          }
        }

        div.style.paddingLeft = (indent*5)+"px";
        Dashboard.Workspace.list_containers.appendChild(div);
        Dashboard.displayContainers(c.getChilds(),indent+1)
    }
}

Dashboard.deleteWorkspace = function()
{
  var currentopt = Dashboard.slt_list.options[Dashboard.slt_list.selectedIndex]
  var id = currentopt.value

  Workspace.deleteWorkspace(id);
  Dashboard.refresh();
}

Dashboard.addContainerToWorkspace = function()
{
  var txt = Dashboard.Workspace.add_container_txt.value
  var workspace = Dashboard.currentWorkspace;
  workspace.addContainer(new SearchContainer(txt));

  Dashboard.refreshWorkspace();
  Dashboard.refreshSelectedWorkspace();
}

Dashboard.refreshSelectedWorkspace = function()
{
  var opt = Dashboard.slt_list.options[Dashboard.slt_list.selectedIndex];
  if (opt)
  {
    var id = opt.value
    Dashboard.currentWorkspace = Workspace.getById(id)
  }
  else
    Dashboard.currentWorkspace = null;
}

Dashboard.refresh = function()
{
  Dashboard.refreshListWorkspace();
  Dashboard.refreshSelectedWorkspace();
  Dashboard.refreshWorkspace();
}

Dashboard.create.submit.onclick = Dashboard.createWorkspace;
Dashboard.slt_list.onchange = Dashboard.selectWorkspace;
Dashboard.Workspace.delete_btn.onclick = Dashboard.deleteWorkspace;
Dashboard.Workspace.add_container_btn.onclick = Dashboard.addContainerToWorkspace;


Dashboard.refresh();